// com.example.task.ActivityStatusTask.java
package com.example.task;

import com.example.mapper.OrganizationActivityMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class ActivityStatusTask {
    
    @Resource
    private OrganizationActivityMapper organizationActivityMapper;
    
    /**
     * 每分钟执行一次状态更新
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void updateActivityStatus() {
        LocalDateTime now = LocalDateTime.now();
        try {
            // 更新未开始→进行中的活动
            organizationActivityMapper.updateStatusToInProgress(now);
            
            // 更新进行中→已结束的活动
            organizationActivityMapper.updateStatusToFinished(now);
            
            log.info("活动状态更新完成: {}", now);
        } catch (Exception e) {
            log.error("活动状态更新失败", e);
        }
    }
}
